def cut(a):
    if len(a) % 2 == 0:
        list1 = a[: len(a) // 2]
        list2 = a[len(a) // 2 :][::-1]
    else:
        list1 = a[: len(a) // 2]
        list2 = a[len(a) // 2 + 1 :][::-1]
    return list1, list2

n = int(input())
list1, list2 = cut(list(map(int, input().split())))

cnt = 0
i = 0
while i < len(list1):
    if list1[i] == list2[i]:
        i += 1
    else:
        cnt += abs(list1[i] - list2[i])
        
        if (list1[i]<list2[i] and list1[i+1]<list2[i+1]) or (list1[i]>list2[i] and list1[i+1]>list2[i+1]):
            if list1[i] < list2[i]:
                list1[i+1] = min(abs(list1[i] - list2[i]) + list1[i+1], list2[i+1])
            else:
                list2[i+1] = min(abs(list1[i] - list2[i]) + list2[i+1], list1[i+1])
        i += 1
            
print(cnt)
